package test.yzx.pproject.act1;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import test.yzx.pproject.R;
import test.yzx.pproject.ViewAnimatorUtil;

public class AnimActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anim_layout1);

      getWindow().getDecorView().post(new Runnable() {
          public void run() {
              final AnimatorSet animSet = new AnimatorSet();

              Animator rotateAnim = makeRotateTopAnim(findViewById(R.id.view));
              Animator scaleAnim = makeScaleSmallAnim(findViewById(R.id.view));
              Animator translateAnim = makeTranslateAnim(findViewById(R.id.view));
              Animator translateBottomAnim = makeBottomTranslateAnim(findViewById(R.id.holder));
              Animator textScaleAnim = makeTextScaleAnim(findViewById(R.id.textLayout));
              Animator textTranslateAnim = makeTextTranslateAnim(findViewById(R.id.textLayout));

              animSet
                      .play(translateAnim)
                      .with(scaleAnim)
                      .with(translateBottomAnim)
                      .with(textScaleAnim)
                      .with(textTranslateAnim)
                      .after(rotateAnim);

              new Handler().postDelayed(new Runnable() {
                  public void run() {
                      animSet.start();
                  }
              }, 500);
          }
      });

    }


    private Animator makeBottomTranslateAnim(final View targetView){
        ValueAnimator anim = ValueAnimator.ofInt(targetView.getMeasuredHeight(), 0).setDuration(800);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                targetView.getLayoutParams().height = (int) animation.getAnimatedValue();
                targetView.requestLayout();
            }
        });
        return anim;
    }
    private Animator makeTranslateAnim(final View target){
        ValueAnimator anim = ValueAnimator.ofInt(0, 100*3).setDuration(700);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                int now = (int) animation.getAnimatedValue();
                target.setTranslationY(-now);
                target.setTranslationX(-now);
            }
        });
        return anim;
    }
    private Animator makeRotateTopAnim(final View target){
        ObjectAnimator anim = ObjectAnimator.ofFloat(target, "rotationY", 0f, 390f, 360f).setDuration(900);
        anim.setInterpolator(new DecelerateInterpolator());
        return anim;
    }
    private Animator makeScaleSmallAnim(final View target){
        ValueAnimator anim = ValueAnimator.ofFloat(1f, 0.5f).setDuration(700);
        anim.setStartDelay(80);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                float now = (float) animation.getAnimatedValue();
                target.setScaleX(now);
                target.setScaleY(now);
            }
        });
        return anim;
    }
    private Animator makeTextTranslateAnim(final View targetView){
        ValueAnimator anim = ValueAnimator.ofInt(0, 220*3).setDuration(700);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                int now = (int) animation.getAnimatedValue();
                targetView.setTranslationY(-now);
            }
        });
        return anim;
    }
    private Animator makeTextScaleAnim(final View targetView){
        return ViewAnimatorUtil.make(700, 0, null, new ViewAnimatorUtil.SimpleValueCallback() {
            public void onUpdate(Animator animation, float value) {
                targetView.setScaleX(value);
                targetView.setScaleY(value);
            }
        }, 1f, 0.7f);
    }

}
